package com.tools.scanner.ui.qrcode

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import com.bumptech.glide.Glide
import com.tools.scanner.BaseActivity
import com.tools.scanner.bean.BarcodeSchema
import com.tools.scanner.database.QrHistory
import com.tools.scanner.databinding.ActivityCodeScannerResultBinding
import com.tools.scanner.ui.qrcode.scanner.BaseScannerFragment

class CodeScannerResultActivity : BaseActivity() {

    companion object {

        private const val SCANNER_HISTORY = "scanner_history"

        fun actionStart(context: Context, qrHistory: QrHistory) {
            val intent = Intent(context, CodeScannerResultActivity::class.java)
            intent.putExtra(SCANNER_HISTORY, qrHistory)
            context.startActivity(intent)
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putSerializable(SCANNER_HISTORY, mQrHistory)
    }

    private lateinit var mBinding: ActivityCodeScannerResultBinding

    private var mQrHistory: QrHistory? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityCodeScannerResultBinding.inflate(layoutInflater)
        setContentView(mBinding.root)

        mQrHistory = if (savedInstanceState != null) {
            savedInstanceState.getSerializable(SCANNER_HISTORY) as QrHistory?
        } else {
            intent.getSerializableExtra(SCANNER_HISTORY) as QrHistory?
        }

        val qrHistory = mQrHistory
        if (qrHistory == null) {
            finish()
            return
        }

        mBinding.ivBack.setOnClickListener {
            finish()
        }

        val fragment = BaseScannerFragment.newInstance(qrHistory)
        supportFragmentManager.beginTransaction().apply {
            replace(mBinding.flContainer.id, fragment, null)
            commitAllowingStateLoss()
        }
    }

}